from time import sleep
import re
import pyperclip

from playwright.sync_api import sync_playwright,Page
from common.config import public_data
from .web_comm import get_token, select_option,excute_js,set_el_value,add_review_doc,add_attachment
from .project_info import project_dic, person_info, get_channel_info, get_department


def create_idrs(channel_dic: dict):

    channel_code = channel_dic["channel_code"]
    title = channel_dic["title"]

    sys_attch : str= channel_dic["sys_attch"]
    review_er = channel_dic["review_er"]

    channel_info = get_channel_info(channel_code)
    prj_code = channel_info["project_code"]
    depart = get_department(review_er)
    comment = channel_dic["comment"]
    is_fax = channel_dic["is_fax"]
    print(depart)


    new_drcs_url = r"http://aed.cnpdc.com/AED/Flow/IDRS/AED_IDRS_Edit.aspx?modi=add"

    if not public_data.Token:
        public_data.Token = get_token()

    if not public_data.Token:
        return False

    new_drcs_url += "&" + public_data.Token
    

    

    with sync_playwright() as p:
        brower = p.chromium.launch(headless=False)
        page = brower.new_page()
        page.goto(new_drcs_url)
        url = page.url
        if r"https://uap.cgnpc.com.cn/" in url:
            try:
                page.wait_for_selector('//*[@id="drcs"]', timeout=60000)
            except Exception as e:
                print("获取验证超时")
                return
        
        base_info(page,title)  # 基础信息
        set_project(page,prj_code) # 设置项目
        set_reviewer(page,depart) # 添加审查者
        other_info(page,channel_code,comment)
        save(page)
        if not is_fax:
            add_review_doc(page,channel_code)
        add_attachment(page,att_file_str=sys_attch)
        save(page)
       
        
def base_info(page:Page,title):

    id_select_secret = "ctl00_ContentPlaceHolder1_AED_IDRS_Edit1_DDL_Doc_SecClass"  # m密级选择
    select_option(page, id_select_secret, "无密级")
    title_txt_id = '#ctl00_ContentPlaceHolder1_AED_IDRS_Edit1_TB_Bas_Name__txt1'
    page.locator(title_txt_id).fill(title)


def set_project(page:Page,prj_code):
    set_el_value(page,"#ctl00_ContentPlaceHolder1_AED_IDRS_Edit1_Input_PrjCode",prj_code)



    set_el_value(page,"#ctl00_ContentPlaceHolder1_AED_IDRS_Edit1_DDL_Bas_PrjCode",prj_code)
def set_reviewer(driver:Page,department):
    # 参与方板块
    review_block = department["block"]
    select_option(driver, "ctl00_ContentPlaceHolder1_AED_IDRS_Edit1_DDL_ToSect", review_block)
    # 所属板块
    select_option(driver, "ctl00_ContentPlaceHolder1_AED_IDRS_Edit1_DDL_Bas_FromSect", "P")
    # 参与方名称
    review_txt_id = '#_easyui_textbox_input7'
    driver.locator(review_txt_id).click()
    combs_xpath = '//div[@id="ctl00_ContentPlaceHolder1_AED_IDRS_Edit1_pzUser_frm1"]/following-sibling::div[1]/div/div'
    combs = driver.query_selector_all(combs_xpath)
    review_depart= department["depart"]
    el = driver.query_selector("[name='ctl00$ContentPlaceHolder1$AED_IDRS_Edit1$ToDepAndKeShi$ddlDepth']")
    if el:
        # el.set_attribute("value",review_depart)
        el.evaluate('node=>node.setAttribute("value","PSE")')

    for select in combs:
        print(select.text_content())
        if review_depart in  select.text_content():
            print(select.text_content())
            select.evaluate('node=>node.setAttribute("class","combobox-item combobox-item-selected")')
            select.scroll_into_view_if_needed()
            select.click()
            break
    # 是否板块间协审
    if review_block == "P":
        driver.locator( '#ctl00_ContentPlaceHolder1_AED_IDRS_Edit1_HasJKR_1').click()
    else:
        driver.locator("#ctl00_ContentPlaceHolder1_AED_IDRS_Edit1_HasJKR_0").click()

def other_info(driver:Page, channel_code,comment):
    select_option(driver,"#stFileTemplate","内部协审单")
    # 文件通道号
    driver.locator('#ctl00_ContentPlaceHolder1_AED_IDRS_Edit1_TB_Otr_TransmitRef__txt1').fill(channel_code)
    
    # 主办方意见
    driver.locator('#ctl00_ContentPlaceHolder1_AED_IDRS_Edit1_TB_Bas_Remark').fill(comment)

def save(page:Page):
    page.locator("#ctl00_ContentPlaceHolder1_BtnSave").click()
    btn_save_confirm_xpath = '//div[@class="dialog-button messager-button"]/a'
    page.locator(btn_save_confirm_xpath).click()
